import GameConst from "../script/js/game/GameConst";
import BitmapText from "../script/js/game/BitmapText";

export default class MapItem extends Laya.Script{
    onAwake(){
        this._lock = this.owner.getChildByName('lock');
        this._star = this.owner.getChildByName('star');
        this._bg = this.owner.getChildByName('bg');
        this._lvlNum = this.owner.getChildByName('num');
        this.text = new BitmapText("","map/");
        this._lvlNum.addChild(this.text);        
    }
    
    onItemRender(){
        
        var source = this.owner.dataSource;
        if(!source){
            return;
        }
        var lvl = source.lvl;
        var star = source.star;
        var lock = source.lock;
        this.init(lvl,star,lock);
    }

    reset(){
        this._star.visible = !this.lock;
        this._bg.visible = !this.lock;
        this._lvlNum.visible = !this.lock;
        this._lock.visible = this.lock;       
        //this.text.x = (this.owner.getBounds().width-bd.width)/2;
        if(!this.lock){
            this.text.text = ""+this.lvl;
            var bd = this.text.getBounds();
            this.text.pivotX = bd.width/2;
            for(var i = 0;i<3;i++){
                this._star.getChildAt(i).texture = i<this.star?"map/star.png":"map/star_bg.png";
            }
        }
    }

    play(delay){
        this.delay = delay;
        this.frame = 0;
        this._play = true;
        return this.star*36;
    }

    onUpdate(){
        if(!this._play){
            return;
        }
        this.frame+=1;
        var t0 = this.frame-this.delay;
        if(t0>0){
            var rted = false;
            for(var i=0;i<this.star;i++){
                var rt = 5*t0-i*180;
                if(rt>0 && rt<=360){
                    var sp = this._star.getChildAt(i);
                    sp.rotation = rt;
                    rted = true;
                }
            }
            if(!rted){
                this._play = false;
            }
        }
        
    }
/**
 * 
 * @param {Number} lvl 关卡等级
 * @param {Number} star  通关星数
 * @param {Boolean} lock  是否锁定关卡
 */
    init(lvl,star,lock){
        this.lvl = lvl;
        this.star = star;
        this.lock = lock;
        this.reset();
    }
}